package com.atom.process.cnmuseum.bm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.atom.common.VariablesConstant;
import com.atom.common.entity.HistoricTaskDO;
import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.PreConditionUtil;
import com.atom.process.cnmuseum.bm.entity.task.BmTaskDO;
import com.atom.process.cnmuseum.bm.tool.TaskDefinitions;
import com.atom.process.cnmuseum.bm.vo.request.GcxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.RequestVO;
import com.atom.process.cnmuseum.bm.vo.request.WhrlxxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.XzxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.YxxxRequestVO;
import com.sinosoft.cnmuseum.utils.LogUtils;

@Service
public class ZiYuanShenheService extends AbstractService implements ServiceInterface {

    @Override
    public void save(RequestVO vo,GcxxRequestVO gcxxvo,XzxxRequestVO xzxxvo, YxxxRequestVO yxxxvo, WhrlxxxRequestVO whrlxxxvo,  UserUtil user) {
        /* 参数校验 */
        paramsCheck(vo);

        String taskId = vo.getTaskId();
        String processInstanceId = vo.getProcessId();
		/* 处理流程数据 */
        String onetitle = "资源审核" + gcxxvo.getCollection_code();
        // 更新ADO
        saveAdo(taskId, vo, gcxxvo, xzxxvo,yxxxvo,whrlxxxvo,user,onetitle);
        // 更新流程变量
        // 生成标题
        String title = "编目_" + gcxxvo.getCollection_code();
        activitiHandler.updateProcessInstanceName(taskId, processInstanceId, title);
    }

    @Override
    public void submit(RequestVO vo, UserUtil user) {
		/* 参数校验 */
        paramsCheck(vo);
        String taskId = vo.getTaskId();
        String mainProcessInstanceId = vo.getProcessId();
        // 完成任务
        Map<String, Object> var_complete = new HashMap<String, Object>();
        var_complete.put(VariablesConstant.PREVIOUS_ASSIGNEE, user.getUserid());
        // 提交录入或审核
        String next = vo.getNext();
        // 更新PDO
        ADO2PDO(mainProcessInstanceId, vo.getTaskId());
		commentDao.saveComment(vo.getYourAdvice(), user, null, 0, taskId, mainProcessInstanceId, TaskDefinitions.资源审核.getKey());
        if (next.equals("bm")) {
            // 获取最后编目人ID
            HistoricTaskDO bmr = historicTaskHandler.getLastByProcessIdAndTaskKey(mainProcessInstanceId, TaskDefinitions.编目.getKey());
            String bmrid = bmr.getAssignee();
            var_complete.put("taskFlowType", next);
            var_complete.put("nextAssignee", bmrid);
        } else {
            String nextids = vo.getNextids();
            var_complete.put("taskFlowType", next);
            var_complete.put("nextAssignee", nextids);
            guidang(mainProcessInstanceId);
        }

		/* 处理流程数据 */
        // 完成任务/写入已办表/删除待办表
        activitiHandler.completeTask(taskId, var_complete);
        //资源审核提交日志
        LogUtils logUtils = new LogUtils();
        String desc = "资源审核"+LogUtils.MbXtglSJBJ_SUBMIT_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_submit), LogUtils.model_sjbj, user.getUserid(), desc);
    }

    private void paramsCheck(RequestVO vo) {
        PreConditionUtil.checkNotNull(vo.getTaskId(), "taskId is null");
        PreConditionUtil.checkNotNull(vo.getProcessId(), "processId is null");
    }
}
